﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Text.RegularExpressions;
using System.Windows.Documents;
using System.Windows.Data;
using System.Windows;
using System.Diagnostics;
using System.Windows.Media;
using System.Windows.Input;

namespace Douban.Core.Converters
{
    public class HtmlContentConverter : IValueConverter
    {
        private const string HyperlinkRegex = "<a href=\"(.*?)\">(.*?)</a>";
        private const string DoubanHomepage = "http://www.douban.com";

        public Style TextStyle { get; set; }
        public Style HyperlinkStyle { get; set; }
        public ICommand HyperlinkCommand { get; set; }

        public TextBlock Parse(string input)
        {
            Regex regex = new Regex(HyperlinkRegex);
            MatchCollection matches = regex.Matches(input);

            TextBlock returnText = new TextBlock();

            if (TextStyle != null) returnText.Style = TextStyle;

            if (matches.Count == 0)
            {
                returnText.Inlines.Add(new Run(input));
            }
            else
            {
                ProcessMatch(input, returnText, matches); 
            }

            return returnText;
        }

        private void ProcessMatch(string input, TextBlock returnText, MatchCollection matches)
        {
            int lastIndex = 0;

            foreach (Match match in matches)
            {
                returnText.Inlines.Add(
                    new Run(input.Substring(lastIndex, match.Index - lastIndex)));

                lastIndex = match.Index + match.Length;

                var hyperlink = new Hyperlink(new Run(match.Groups[2].Value));
                hyperlink.NavigateUri = new Uri(
                    match.Groups[1].Value == "#" ? DoubanHomepage : match.Groups[1].Value);

                BindingCommand(hyperlink);
                BindingStyle(hyperlink);

                returnText.Inlines.Add(hyperlink);
            }
        }

        private void BindingStyle(Hyperlink hyperlink)
        {
            if (HyperlinkStyle != null) hyperlink.Style = HyperlinkStyle;
        }

        private void BindingCommand(Hyperlink hyperlink)
        {
            if (HyperlinkCommand != null)
            {
                hyperlink.Command = HyperlinkCommand;
            }
            else
            {
                hyperlink.Click += new RoutedEventHandler(hyperLink_Click);
            }
        }

        private void hyperLink_Click(object sender, RoutedEventArgs e)
        {
            Hyperlink link = (Hyperlink)sender;
            Process.Start(link.NavigateUri.AbsoluteUri);
        }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return new TextBlock();

            return Parse(value.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
